package models

import (
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(50)"json:"username"`
	Password string `gorm:"type:varchar(50)"json:"password"`
}

type UserRoldgxb struct {
	gorm.Model
	Uid int `gorm:"type:char(11)"json:"uid"`
	Rid int `gorm:"type:char(11)"json:"rid"`
}

type Rold struct {
	gorm.Model
	RoldName string `gorm:"type:varchar(50)"json:"roldName"`
}

type RoldQuangxb struct {
	gorm.Model
	Rid int `gorm:"type:char(11)"json:"rid"`
	Qid int `gorm:"type:char(11)"json:"qid"`
}

type Quan struct {
	gorm.Model
	QianName string `gorm:"type:varchar(50)"json:"QianName"`
}

type Compan struct {
	gorm.Model
	Companname string `gorm:"type:varchar(50)"json:"companame"`
}

type Types struct {
	gorm.Model
	Typess string `gorm:"type:varchar(50)"json:"typess"`
}

type Orders struct {
	gorm.Model
	Ordername string `gorm:"type:varchar(50)"json:"ordername"`
	Cid       int    `gorm:"type:char(11)"json:"cid"`
	Tid       int    `gorm:"type:char(11)"json:"tid"`
}

func (*User) DataAdd(username string, user *User) error {
	return DB.Where("username=?", username).First(&user).Error
}

func (*RoldQuangxb) RoldQuangxbAdd(data *RoldQuangxb) (int, error) {
	tx := DB.Begin()
	sreutl := DB.Debug().Create(&data)
	if sreutl.Error != nil {
		tx.Rollback()
		return 0, sreutl.Error
	}
	tx.Commit()
	return int(data.ID), nil
}

func (*Rold) List(uid int) (list []Rold, err error) {
	err = DB.Table("rolds").
		Joins("left join user_roldgxbs on user_roldgxbs.rid=rolds.id").
		Where("user_roldgxbs.uid=?", uid).
		Find(&list).Error
	return list, err
}

func (*Quan) QuanList(rid int) (lists []Quan, err error) {
	err = DB.Table("quans").
		Joins("left join rold_quangxbs on rold_quangxbs.qid=quans.id").
		Where("rold_quangxbs.rid=?", rid).
		Find(&lists).Error
	return lists, err
}

func (*Compan) CompanAdd(data *Compan) (int, error) {
	tx := DB.Begin()
	sreutl := DB.Debug().Create(&data)
	if sreutl.Error != nil {
		tx.Rollback()
		return 0, sreutl.Error
	}
	tx.Commit()
	return int(data.ID), nil
}
func (*Orders) OrdersAdd(data *Orders) (int, error) {
	tx := DB.Begin()
	sreutl := DB.Debug().Create(&data)
	if sreutl.Error != nil {
		tx.Rollback()
		return 0, sreutl.Error
	}
	tx.Commit()
	return int(data.ID), nil
}

type Ordersss struct {
	Id         int    `json:"id"`
	Ordername  string `json:"ordername"`
	Companname string `json:"companname"`
	Typess     string `json:"typess"`
}

func (*Orders) OrderDatalist(id int) (orders Orders, err error) {
	err = DB.Where("id=?", id).First(&orders).Error
	return orders, err
}
